﻿using System;
using System.Collections.Generic;
using System.Text;

using IBatisNet.Common.Utilities;
using IBatisNet.DataAccess;
using IBatisNet.DataAccess.Configuration;
using System.Configuration;
using System.IO;
namespace AUV5.DAL
{
    public class ServiceConfig
    {
        static private object _synRoot = new Object();
        static private ServiceConfig _instance;

        private IDaoManager _daoManager = null;

        /// <summary>
        /// Remove public constructor. prevent instantiation.
        /// </summary>
        private ServiceConfig() { }

        public static ServiceConfig Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_synRoot)
                    {
                        if (_instance == null)
                        {
                            try
                            {
                                //定义ConfigureHander事件的一个实例handler，并调用Service.Reset作为参数，该事件的作用是
                                ConfigureHandler handler = new ConfigureHandler(ServiceConfig.Reset);
                                DomDaoManagerBuilder builder = new DomDaoManagerBuilder();
                                builder.ConfigureAndWatch("dao.config",handler);
                                _instance = new ServiceConfig();   //如果Instance为空，就创建一个类实例，并把值赋给存储类实例的变量instance. 
                                //返回一个DaoManager实例，"SqlMapDao"为dao.config文件 context 节点id的值
                                _instance._daoManager = IBatisNet.DataAccess.DaoManager.GetInstance("SqlMapDao");  
                            }
                            catch (Exception e)
                            {
                                throw (e);
                            }
                        }
                    }
                }
                return _instance;
            }
        }


        /// <summary>
        /// Reset the singleton
        /// </summary>
        /// <remarks>
        /// Must verify ConfigureHandler signature.
        /// </remarks>
        /// <param name="obj">
        /// </param>
        static public void Reset(object obj)
        {
            _instance = null;
        }

        public IDaoManager DaoManager
        {
            get
            {
                return _instance._daoManager;
            }
        }

    }
}
